<!--
 * @Author: your name
 * @Date: 2021-04-10 20:19:30
 * @LastEditTime: 2021-06-01 18:30:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ws-shop\src\views\Locations.vue
-->
<template>
  <div class="location">
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @select="onChange"
    />
    <van-address-edit
      id="add"
      style="display:none;"
      :address-info="AddressInfo"
      :area-list="areaList"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    />
  </div>
</template>

<script>
import Vue from 'vue'
import { AddressList, AddressEdit } from 'vant'
import { areaList } from '@vant/area-data'
Vue.use(AddressList)
Vue.use(AddressEdit)

export default {
  data () {
    return {
      AddressInfo:
      {
        id: '',
        name: '',
        tel: '',
        addressDetail: ''
      },
      areaList,
      chosenAddressId: '1',
      list: [
        // JSON.parse(localStorage.getItem('Address'))
        {
          id: '1',
          name: '张三',
          tel: '13000000000',
          address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
          isDefault: true
        }
      ]
    }
  },
  methods: {
    onSave (content) {
      this.AddressInfo.id = content.areaCode
      this.AddressInfo.name = content.name
      this.AddressInfo.tel = content.tel
      this.AddressInfo.address = content.addressDetail
      localStorage.setItem('Address', JSON.stringify(this.AddressInfo))
      this.list.push(JSON.parse(localStorage.getItem('Address')))
      document.getElementById('add').style.display = 'none'
    },
    onChange () {
      this.$router.push({
        path: '/ent'
      })
    },
    onAdd () {
      document.getElementById('add').style.display = 'block'
    }
  }
}
</script>

<style lang="scss" scoped>
.van-address-edit {
  width: 100%;
  height: 100%;
  padding:0;
  z-index: 100;
  margin-top: -170px;
}
</style>
